package com.taxi.driver.module.order.cancel;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.amap.api.maps.model.LatLng;
import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.PositionType;
import com.taxi.driver.data.amap.AMapManager;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.module.vo.CancelDesVO;
import com.taxi.driver.module.vo.CancelReasonVO;
import com.taxi.driver.socket.message.UploadLocationMessage;
import com.taxi.driver.socket.message.base.MessageType;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class OrderCancelPresenter extends BasePresenter implements OrderCancelContract.Presenter {

    private final OrderCancelContract.View mView;
    private final UserRepository mUserRepository;
    private final OrderRepository mOrderRepository;
    private final AMapManager mAMapManager;

    private String mOrderUuid;

    @Inject
    public OrderCancelPresenter(OrderCancelContract.View view, UserRepository userRepository,
                                OrderRepository orderRepository, AMapManager aMapManager) {
        mView = view;
        mUserRepository = userRepository;
        mOrderRepository = orderRepository;
        mAMapManager = aMapManager;
    }

    @Override
    public void subscribe() {
        super.subscribe();
        if (mFirstSubscribe) {
            reqCancelMsg(); //获取取消标签
            reqCancelDes(); //获取取消说明
        }
    }

    @Override
    public void setOrderUuid(String orderUuid) {
        mOrderUuid = orderUuid;
    }

    @Override
    public String getOrderUuid() {
        return mOrderUuid;
    }

    @Override
    public void reqCancelDes() {
        mSubscriptions.add(
                mOrderRepository.cancelDescription(mOrderUuid)
                        .map(CancelDesVO::createFrom)
                        .compose(RxUtil.applySchedulers())
                        .subscribe(mView::showCancelDes, KLog::e));
    }

    @Override
    public void reqCancelMsg() {
        mSubscriptions.add(
                mUserRepository.reqCancelMsg()
                        .flatMapIterable(entities -> entities)
                        .map(CancelReasonVO::createFrom)
                        .toList()
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(mView::hideLoadingView)
                        .subscribe(mView::showCancelMsg,
                                ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository))
        );
    }

    @Override
    public void submitCancel(String content) {
        if (TextUtils.isEmpty(content)) {
            toast("请选择取消原因");
            return;
        }

        LatLng latLng = new LatLng(mAMapManager.getLastLocation().getLatitude(), mAMapManager.getLastLocation().getLongitude());
        String uploadText = uploadText(BizStatus.EMPTY_DRIVING, latLng, PositionType.SJQX);

        mSubscriptions.add(
                mOrderRepository.reqCancelOrder(mOrderUuid, content, uploadText)
                        .compose(RxUtil.applySchedulers())
                        .doOnSubscribe(() -> mView.showLoadingView(true))
                        .doAfterTerminate(mView::hideLoadingView)
                        .subscribe(s -> mView.cancelSuccess(mOrderUuid),
                                ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository)));
    }

    private String uploadText(int bizStatus, LatLng latLng, String positionType) {
        AMapLocation lastLocation = mAMapManager.getLastLocation();
        UploadLocationMessage uploadLocationMessage = new UploadLocationMessage();
        uploadLocationMessage.setAppid(((Activity) mView).getString(R.string.yueyue_appkey));
        uploadLocationMessage.setBizStatus(bizStatus);
        uploadLocationMessage.setDriverType(mUserRepository.getDriverType());
        uploadLocationMessage.setOrderUuid(mOrderUuid);
        uploadLocationMessage.setAreaCode(lastLocation.getAdCode());
        uploadLocationMessage.setLat(latLng.latitude);
        uploadLocationMessage.setLng(latLng.longitude);
        uploadLocationMessage.setPositionType(positionType);
        uploadLocationMessage.setDistance(0d);
        uploadLocationMessage.setAngle(lastLocation.getBearing());
        uploadLocationMessage.setSpeed(lastLocation.getSpeed());
        uploadLocationMessage.setElevation(lastLocation.getAltitude());
        uploadLocationMessage.setAccuracy(lastLocation.getAccuracy());
        uploadLocationMessage.setCarLevelType(mUserRepository.getCarLevelType());
        uploadLocationMessage.setClientUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setDriverUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setLocationUuid(System.currentTimeMillis() + "");
        uploadLocationMessage.setType(MessageType.UPLOAD_LOCATION);
        return JSON.toJSONString(uploadLocationMessage);
    }
}
